import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { User } from '../model/user';
import { HttpServer } from '@core/net/http-server';
import { PageInfo } from '@core/model/pageInfo';
import { UrlManager } from '@shared/url/url.manager';


@Injectable()
export class UserService {
  constructor(
    private httpServer: HttpServer
  ) { }

  private _user: User = new User();

  get user(): User {
    return this._user;
  }

  // setUser(val: User) {
  //   this._user = Object.assign(this._user, val);
  // }
  //
  // /**
  //  * userLogin
  //  * @param accout 账号
  //  * @param password 密码
  //  * @return token
  //  */
  // userLogin(account: string, password: string, verify: string, verifyToken: string): Promise<string> {
  //   const headerParam: Array<HeaderParam> = [{
  //     key: 'Content-Type',
  //     value: 'application/json'
  //   }, {
  //     key: 'Authorization',
  //     value: verifyToken
  //   }]
  //   return this.httpServer.post(UrlManager.user.userLoginUrl, {
  //     account: account,
  //     password: password,
  //     verify: verify
  //   }, headerParam, false).then(response => {
  //     return response;
  //   })
  //     .catch(this.httpServer.handleError);
  // }
  //
  // /**
  //  * 获取验证码
  //  * @return {Promise<string>} [description]
  //  */
  // verify(): Promise<Validate> {
  //   return this.httpServer.get(UrlManager.user.verifyUrl, {
  //     time: new Date().getTime()
  //   }, false)
  //     .then(response => {
  //       return response as Validate;
  //     });
  // }
  //
  // logout(): Promise<boolean> {
  //   return this.httpServer.get(UrlManager.user.logoutUrl, {})
  //     .then(response => {
  //       return true;
  //     })
  //     .catch(this.httpServer.handleError);
  // }
  //
  // getUserInfo(): Promise<User> {
  //   return this.httpServer.get(UrlManager.user.getUserInfo, {})
  //     .then(response => response as User)
  //     .catch(this.httpServer.handleError);
  // }
  //
  /**
   * [getUserByName description]
   * @param  {string}        userName [description]
   * @return {Promise<User>}          [description]
   */
  getUserByName(userName: string): Observable<User> {
    return this.httpServer.get(UrlManager.user.getUserByNameUrl, { account: userName });
  }

  getAllUsers(pageNo: number, pageSize: number): Observable<PageInfo<User>> {
    return this.httpServer.get(UrlManager.user.getAllUsersUrl, {
      pageNo: pageNo,
      pageSize: pageSize
    });
  }

  getUserById(userId: number): Observable<User> {
    return this.httpServer.get(UrlManager.user.getUserByIdUrl, { id: userId });
  }

  addUser(user: User): Observable<boolean> {
    return this.httpServer.post(UrlManager.user.addUserUrl, user);
  }

  encriptPwd(user: User): Observable<string> {
    return this.httpServer.post(UrlManager.user.encryptPwdPUrl, user);
  }

  updateUser(user: User): Observable<boolean> {
    return this.httpServer.post(UrlManager.user.uppdateUserUrl, user);
  }

  deltetUser(id: number): Observable<boolean> {
    return this.httpServer.get(UrlManager.user.deleteUserUrl, {
      id: id
    });
  }

  getUserNameCount(accout: string): Observable<User> {
    return this.httpServer.get(UrlManager.user.getUserNameCount, {
      account: accout
    });
  }
}
